package com.nand.blockdemo.ui.feedback;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.lzy.okgo.model.HttpParams;
import com.nand.blockdemo.R;
import com.nand.blockdemo.adapter.FeekAdapter;
import com.nand.blockdemo.bean.FeekItemBean;
import com.nand.blockdemo.bean.FeekListBean;
import com.nand.blockdemo.bean.MenuBean;
import com.nand.blockdemo.bean.ModulePageBean;
import com.nand.blockdemo.constant.URL;
import com.nand.blockdemo.ui.view.DropDownMenuPopupView;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.ObjectUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.module.function.utils.LogUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class FeekActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView mRecyclerView;

    private List<MenuBean> list = new ArrayList<>();
    private List<MenuBean> portList = new ArrayList<>();
    private List<MenuBean> moduleList = new ArrayList<>();
    private List<MenuBean> pageList = new ArrayList<>();
    private List<List<MenuBean>> modulePageList = new ArrayList<>();
    private List<MenuBean> stateList = new ArrayList<>();
    private DropDownMenuPopupView portPopupView, statePopupView, modulePopupView, pagePopupView;
    private SmartRefreshLayout mRefreshLayout;
    private FeekAdapter mAdapter;
    private ConstraintLayout mNoDataLayout;

    private int userId;
    private int page = 0;

    private boolean flag = true;
    private TextView tvNoData;
    private EditText mTextNum;
    private int isHandle = 0;
    private TextView mTextPort;
    private TextView mTextModule;
    private TextView mTextPage;
    private TextView mTextState;
    private MenuBean moduleItem;
    private MenuBean portItem;
    private MenuBean pageItem;
    private MenuBean stateItem;
    private String strNumber;

    @Override
    protected boolean isActionBar() {
        return true;
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_feek;
    }

    @Override
    protected void initView() {
        mActionBar().setCenterText("意见反馈");
        mActionBar().setRightIcon(R.mipmap.add_photo, clickListener);
        findViewById(R.id.button).setOnClickListener(clickListener);

        mTextPort = findViewById(R.id.tv_port);
        mTextModule = findViewById(R.id.tv_module);
        mTextPage = findViewById(R.id.tv_page);
        mTextState = findViewById(R.id.tv_state);
        mTextPort.setOnClickListener(this);
        mTextModule.setOnClickListener(this);
        mTextPage.setOnClickListener(this);
        mTextState.setOnClickListener(this);
        findViewById(R.id.tv_query).setOnClickListener(this);

        tvNoData = findViewById(R.id.tv_no_data);

        mTextNum = findViewById(R.id.et_number);

        mNoDataLayout = findViewById(R.id.constraintLayout);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new FeekAdapter();
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FeekItemBean data = (FeekItemBean) adapter.getData().get(position);
                Intent intent = new Intent(FeekActivity.this, FeekDetailsActivity.class);
                intent.putExtra("oid", data.getOid() + "");
                intent.putExtra("isHandle", isHandle);
                startActivity(intent);
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getHttpData();
            }
        }, mRecyclerView);


        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                getHttpData();
            }
        });

    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(FeekActivity.this, FeekEditActivity.class);
            startActivity(intent);
        }
    };


    @Override
    protected void initData() {
        userId = SpUtils.readInt(this, "userId", 0);
        List<String> setAuth = SpUtils.getListData("setAuth", String.class);
        for (int i = 0; i < setAuth.size(); i++) {
            if ("系统模块-反馈-处理".equals(setAuth.get(i))) {
                isHandle = 1;
            }
        }

        portList.add(new MenuBean("移动端", 0));
        portList.add(new MenuBean("PC端", 1));
        stateList.add(new MenuBean("待处理", 1));
        stateList.add(new MenuBean("待确定", 2));
        stateList.add(new MenuBean("已完成", 3));

        for (int i = 0; i < 8; i++) {
            list.add(new MenuBean("选项:" + i, i));
        }
        OkGoUtils.getInstance().post(this, URL.GET_MODULE_PAGE, null, 1, false, mCallback);
    }

    @Override
    protected void onStart() {
        super.onStart();
        page = 0;
        getHttpData();
    }
    private void getHttpData() {
        page++;
        HttpParams params = new HttpParams();
        if(isHandle == 0){
            params.put("userId", userId);
        }
        params.put("current", page);
        params.put("size", "10");

        if (ObjectUtils.notEmpty(strNumber)) {
            params.put("feedbackoid", strNumber);
        } else {
            if (portItem != null) {
                params.put("type", portItem.getTitle());
            }
            if (moduleItem != null) {
                params.put("moduleOid", moduleItem.getId());
            }
            if (pageItem != null) {
                params.put("pageOid", pageItem.getId());
            }
            if (stateItem != null) {
                params.put("state", stateItem.getId());
            }
        }
        OkGoUtils.getInstance().post(this, URL.FEEDBACK_LIST_URL, params, 0, false, mCallback);
    }

    private MyCallback mCallback = new MyCallback() {
        @Override
        public void onSuccess(ResponseBean response, int id) {
            if (response.getCode() == 200) {
                String str = response.getDataInfo().toString();
                switch (id) {
                    case 0:
                        FeekListBean listBean = JSON.parseObject(str, FeekListBean.class);
                        List<FeekItemBean> lists = listBean.getDataList();
                        if (page > 1) {
                            if (ObjectUtils.notEmpty(lists) && lists.size() == 10) {
                                //加载更多成功
                                mAdapter.addData(lists);
                                mAdapter.loadMoreComplete();
                                LogUtils.e("加载更多成功");
                            } else {
                                //无更多数据加载
                                LogUtils.e("无更多数据加载");
                                mAdapter.loadMoreEnd();
                            }
                        } else {
                            if (ObjectUtils.notEmpty(lists)) {
                                //刷新成功
                                LogUtils.e("刷新成功");
                                mAdapter.setNewData(lists);
                                mRefreshLayout.finishRefresh();
                                if (flag) {
                                    mNoDataLayout.setVisibility(View.GONE);
                                    flag = false;
                                } else {
                                    tvNoData.setVisibility(View.GONE);
                                }
                            } else {
                                //无数据
                                LogUtils.e("无数据");
                                if (flag) {
                                    mRefreshLayout.setVisibility(View.GONE);
                                    mNoDataLayout.setVisibility(View.VISIBLE);
                                    flag = false;
                                } else {
                                    mAdapter.setNewData(lists);
                                    mAdapter.loadMoreEnd();
                                    tvNoData.setVisibility(View.VISIBLE);
                                }
                            }
                        }
                        break;
                    case 1:
                        List<ModulePageBean> modulePageList = JSON.parseArray(str, ModulePageBean.class);
                        setModulePageData(modulePageList);
                        break;
                }
            } else {
                //加载失败
                showToast(response.getMsg());
                page--;
                mAdapter.loadMoreFail();
                mRefreshLayout.finishRefresh();
            }
        }

        @Override
        public void onError(Throwable e, int id) {

        }
    };

    private void setModulePageData(List<ModulePageBean> list) {
        for (int i = 0; i < list.size(); i++) {
            ModulePageBean module = list.get(i);
            List<ModulePageBean.AuthorityListBean> list1 = module.getAuthorityList();
            for (int i1 = 0; i1 < list1.size(); i1++) {
                pageList.add(new MenuBean(list1.get(i1).getYeTitle(), Integer.parseInt(list1.get(i1).getYeOid())));
            }
            moduleList.add(new MenuBean(module.getTitle(), Integer.parseInt(module.getOid())));
        }
    }


    /**
     * 功能模块弹出框
     *
     * @param v
     */
    private void showModuleMenu(final View v) {
        if (modulePopupView == null) {
            modulePopupView = (DropDownMenuPopupView) new XPopup.Builder(this)
                    .atView(v)
                    .autoOpenSoftInput(true)
                    .asCustom(new DropDownMenuPopupView(this, moduleList));
            modulePopupView.setOnItemClickListener(new DropDownMenuPopupView.OnItemClickListener() {

                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    moduleItem = (MenuBean) adapter.getData().get(position);
                    mTextModule.setText(getText(moduleItem.getTitle()));
                    modulePopupView.dismiss();
                    setHttp();
                }
            });
        }
        modulePopupView.show();
    }

    /**
     * 端口菜单弹出框
     *
     * @param v
     */
    private void showPortMenu(final View v) {
        if (portPopupView == null) {
            portPopupView = (DropDownMenuPopupView) new XPopup.Builder(this)
                    .atView(v)
                    .autoOpenSoftInput(true)
                    .asCustom(new DropDownMenuPopupView(this, portList));
            portPopupView.setOnItemClickListener(new DropDownMenuPopupView.OnItemClickListener() {

                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    portItem = (MenuBean) adapter.getData().get(position);
                    mTextPort.setText(portItem.getTitle());
                    portPopupView.dismiss();
                    setHttp();
                }
            });
        }
        portPopupView.show();
    }

    /**
     * 功能页面弹出框
     *
     * @param v
     */
    private void showPageMenu(final View v) {
        if (pagePopupView == null) {
            pagePopupView = (DropDownMenuPopupView) new XPopup.Builder(this)
                    .atView(v)
                    .autoOpenSoftInput(true)
                    .asCustom(new DropDownMenuPopupView(this, pageList));
            pagePopupView.setOnItemClickListener(new DropDownMenuPopupView.OnItemClickListener() {

                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    pageItem = (MenuBean) adapter.getData().get(position);
                    mTextPage.setText(getText(pageItem.getTitle()));
                    pagePopupView.dismiss();
                    setHttp();
                }
            });
        }
        pagePopupView.show();
    }

    /**
     * 状态菜单弹出框
     *
     * @param v
     */
    private void showStateMenu(final View v) {
        if (statePopupView == null) {
            statePopupView = (DropDownMenuPopupView) new XPopup.Builder(this)
                    .atView(v)
                    .autoOpenSoftInput(true)
                    .asCustom(new DropDownMenuPopupView(this, stateList));
            statePopupView.setOnItemClickListener(new DropDownMenuPopupView.OnItemClickListener() {

                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    stateItem = (MenuBean) adapter.getData().get(position);
                    mTextState.setText(stateItem.getTitle());
                    statePopupView.dismiss();
                    setHttp();
                }
            });
        }
        statePopupView.show();
    }

    public void setHttp() {
        page = 0;
        strNumber = "";
        mTextNum.setText(strNumber);
        getHttpData();
    }

    public String getText(String str) {
        if (str.length() > 4) {
            str = str.substring(0, 2) + "..." + str.substring(str.length() - 2, str.length());
        }
        return str;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_port:
                showPortMenu(view);
                break;
            case R.id.tv_module:
                showModuleMenu(view);
                break;
            case R.id.tv_page:
                showPageMenu(view);
                break;
            case R.id.tv_state:
                showStateMenu(view);
                break;
            case R.id.tv_query:
                strNumber = mTextNum.getText().toString().trim();

                portItem = null;
                moduleItem = null;
                pageItem = null;
                stateItem = null;

                mTextPort.setText("问题平台");
                mTextModule.setText("功能模块");
                mTextPage.setText("功能界面");
                mTextState.setText("状态");

                if (portPopupView != null) {
                    portPopupView.noSelect();
                }
                if (modulePopupView != null) {
                    modulePopupView.noSelect();
                }
                if (pagePopupView != null) {
                    pagePopupView.noSelect();
                }
                if (statePopupView != null) {
                    statePopupView.noSelect();
                }
                page = 0;
                getHttpData();
                break;
        }
    }
}

